﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PocketEngine.Engine;
using PocketEngine.Images;
using PocketEngine.Lights;
using FastMath;
using PocketEngine.Physics;

namespace PocketEngine.Maps
{
    public class pMap
    {
        public List<pLight> Lights = new List<pLight>();
        public List<pLight> OtherLights = null;
        public pTile[,,] Tiles = null;
        public pEntity[,] Entities = null;
        public void Add(pLight light)
        {
            Lights.Add(light);
        }

        public int TilesX
        {
            get
            {
                return mTilesX;
            }
        }
        private int mTilesX = 0;
        
        public int TilesY
        {
            get
            {
                return mTilesY;
            }
        }
        private int mTilesY = 0;

        public int TilesZ
        {
            get
            {
                return mTilesZ;
            }
        }
        private int mTilesZ = 0;

        public int TileWidth
        {
            get
            {
                return mTileWidth;
            }
        }
        private int mTileWidth = 0;

        public int TileHeight
        {
            get
            {
                return mTileHeight;
            }
        }
        private int mTileHeight;

        public pPickResult LinePick(Ray ray,int z)
        {

            return null;
        }

        public pMap(int tilesX, int tilesY, int tilesZ, int tileWidth, int tileHeight)
        {
            Tiles = new pTile[tilesX, tilesY,tilesZ];
            Entities = new pEntity[tilesX, tilesY];
            mTilesX = tilesX;
            mTilesY = tilesY;
            mTilesZ = tilesZ;
            mTileWidth = tileWidth;
            mTileHeight = tileHeight;
        }

        /// <summary>
        /// Call this ONCE, AFTER you have set up or loaded your map fully. it is safe to recall if you make any changes however, no harm will come from it, just memory dedregation
        /// </summary>
        public void InitPhysics()
        {
            for (int x = 0; x < TilesX; x++)
            {
                for (int y = 0; y < TilesY; y++)
                {
                    if (Tiles[x, y, 0]!=null && Tiles[x, y, 0].Blocker)
                    {
                        pBody b = new pBoxBody(TileWidth, TileHeight, 1);
                        pGeom g = new pBoxGeom(b as pBoxBody);
                        //pEngine.Physics.AddBody(b);
                        b.Position = new PocketEngine.Maths.pVec2((x * TileWidth) + TileWidth / 2, (y * TileHeight) + TileHeight / 2);
                        //pEngine.Physics.AddGeom(g);
                        b.Body.IsStatic = true;
                        Entities[x, y] = new pEntity(b, g);
                    }

                }
            }
        }

        public void Clear()
        {
            Tiles = new pTile[TilesX, TilesY,TilesZ];
        }
        public void Rect(pTile tile, int x, int y, int w, int h, int z)
        {
            int dx = x;
            int dy = y;
            while (true)
            {
                Tiles[dx, dy, z] = tile;
                dx++;
                if (dx == (x + w))
                {
                    dx = x;
                    dy++;
                    if (dy == (y + h))
                    {
                        break;
                    }
                }
            }

        }
        public void Fill(pTile tile,int z)
        {
            for (int x = 0; x < TilesX; x++)
            {
                for (int y = 0; y < TilesY; y++)
                {
                    Tiles[x, y, z] = tile;
                }
            }
        }


    }
    public class pPickResult
    {
        public Vector3 Origin;
        public Vector3 ContactNormal;
        public Vector3 ContactPosition;
        public float ContactDepth;
        public int MapX, MapY, MapZ;
        public pMap Map;
        public pTile ContactTile;
    }
}
